package com.ego.item.service;

import com.ego.common.PageResult;
import com.ego.item.mapper.BrandMapper;
import com.ego.item.pojo.Brand;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 实现品牌分页查询
     * @param pageNo
     * @param pageSize
     * @param sortBy
     * @param descending
     * @param key
     * @return
     */
    @Override
    public PageResult<Brand> page(Integer pageNo, Integer pageSize, String sortBy, Boolean descending, String key) {
        //1,分页                第几页开始，每页显示多少条
        PageHelper.startPage(pageNo,pageSize);
        //2,设置查询条件
        Example example = new Example(Brand.class);
        //创建查询对象
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(key)){
            //模糊查询  大小写均可
            criteria.andLike("name", "%"+key+"%").orEqualTo("letter",key.toUpperCase());
        }
        //3,排序  降序/升序
        if (StringUtils.isNotBlank(sortBy)){
            example.setOrderByClause(sortBy+" "+(descending?"desc":"asc"));
        }
        Page<Brand> page = (Page<Brand>) brandMapper.selectByExample(example);
        return new PageResult<Brand>(page.getTotal(),page.getResult());
    }

    /**
     * 保存新增品牌
     * @param brand  品牌对象
     * @param cids   类别ids(可以多个类别)
     */
    @Transactional
    @Override
    public void save(Brand brand, List<Integer> cids) {
        //1,保存品牌(insertSelective只会修改brand中有数据的字段，insert会全部修改)
        brandMapper.insertSelective(brand);

        //2,维护品牌和类别的关系
        if (cids!=null){
            cids.forEach(cid->{
                brandMapper.insertCategoryBrand(cid,brand.getId());
            });
        }
    }

    /**
     * 更新品牌信息
     * 1,更新brand表
     * 2,删除brand和category的关系
     * 3,新增brand和category的关系
     * @param brand
     * @param cids
     */
    @Transactional
    @Override
    public void update(Brand brand, List<Integer> cids) {
        //1,更新brand表
        brandMapper.updateByPrimaryKey(brand);

        //2,删除brand和category的关系  根据bid删除
        brandMapper.deleteCatrgoryBrand(brand.getId());

        //3,新增brand和category的关系  参考之前新增操作
        if (cids!=null){
            cids.forEach(cid->{
                brandMapper.insertCategoryBrand(cid,brand.getId());
            });
        }
    }

    /**
     * 根据id删除品牌信息
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Brand findByBid(Long brandId) {
        return brandMapper.selectByPrimaryKey(brandId);
    }

    @Override
    public List<Brand> findByCid(Long cid) {
        return brandMapper.selectByCid(cid);
    }


}
